package com.itheima.chapter10.thread.demo13;

import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable implements Runnable {
    private int ticket = 100;
    Object obj = new Object();
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {

        while (true) {

            try {
                lock.lock();
                if (ticket > 0) {
                    break;
                } else {
                    Thread.sleep(30);
                    System.out.println(Thread.currentThread().getName() + "--->正在读第" + ticket + "页");
                    ticket--;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }


        }
    }

}